package com.xckj.cycling.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.xckj.cycling.R;
import com.xckj.cycling.databinding.ActivityPerfectRegisterBinding;
import com.xckj.cycling.dialog.RegionDialog;
import com.xckj.cycling.framework.BaseActivity;
import com.xckj.cycling.iview.IPerfectRegisterView;
import com.xckj.cycling.presenter.PerfectRegisterPresenter;
import com.xckj.cycling.utils.AndroidUtil;
import com.xckj.cycling.view.dialog.DateDialog;
import com.xckj.cycling.view.dialog.GenderDialog;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

/**
 * @Author: ChenYao
 * @Describe: 完善注册
 * @Date: 2023/3/1 23:42
 */
public class PerfectRegisterActivity extends BaseActivity<ActivityPerfectRegisterBinding> implements IPerfectRegisterView {
    private PerfectRegisterPresenter perfectRegisterPresenter;
    private int mGender;
    private String mAge;

    @Override
    protected int loadLayout() {
        return R.layout.activity_perfect_register;
    }

    @Override
    protected void setTitle() {
        super.setTitle();
        dataBinding.viewTitle.tvTitle.setText(getString(R.string.perfect_register));
    }

    @Override
    protected void initData() {
        perfectRegisterPresenter = new PerfectRegisterPresenter(this);
    }

    @Override
    protected void setListener() {
        dataBinding.rlSelectAge.setOnClickListener(this);
        dataBinding.rlSelectGender.setOnClickListener(this);
        dataBinding.btnSubmit.setOnClickListener(this);
        dataBinding.etName.addTextChangedListener(textWatcher);
        dataBinding.etWeight.addTextChangedListener(textWatcher);
        dataBinding.tvAge.addTextChangedListener(textWatcher);
        dataBinding.tvGender.addTextChangedListener(textWatcher);
        dataBinding.etCity.setOnClickListener(this);
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            checkButton();
        }
    };

    private void checkButton() {
        boolean b = isNotEmpty(dataBinding.etName) && isNotEmpty(dataBinding.etWeight) && isNotEmpty(dataBinding.tvAge)
                && isNotEmpty(dataBinding.tvGender);
        dataBinding.btnSubmit.setEnabled(b);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        if (AndroidUtil.isFastDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.rl_select_age:
                showSelectAgeDialog();
                break;
            case R.id.rl_select_gender:
                showSelectGenderDialog();
                break;
            case R.id.btn_submit:
                submit();
                break;
            case R.id.et_city:
                new RegionDialog(this, new Function1<String, Unit>() {
                    @Override
                    public Unit invoke(String s) {
                        dataBinding.etCity.setText(s);
                        return Unit.INSTANCE;
                    }
                }).show();
                break;
            default:
                break;
        }
    }

    private void showSelectAgeDialog() {
        new DateDialog.Builder(this).setListener(new DateDialog.MyOnClickListener() {
            @Override
            public void onSelected(String age) {
                dataBinding.tvAge.setText(age);
                mAge = age;
            }

        }).show();
    }

    private void showSelectGenderDialog() {
        new GenderDialog(this).setSelectedGenderClickListener(new GenderDialog.SelectedGenderOnClickListener() {
            @Override
            public void onSelectedGender(int gender) {
                mGender = gender;
                if (1 == gender) {
                    dataBinding.tvGender.setText(R.string.gender_male);
                } else if (0 == gender) {
                    dataBinding.tvGender.setText(R.string.gender_famale);
                } else {
                    dataBinding.tvGender.setText(R.string.gender_other);
                }

            }
        }).build().show();
    }


    private void submit() {
        String name = dataBinding.etName.getText().toString();
        String age = dataBinding.tvAge.getText().toString();
        String gender = dataBinding.tvGender.getText().toString();
        String weight = dataBinding.etWeight.getText().toString();
        String city = dataBinding.etCity.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showMessage(getString(R.string.name_hint));
            return;
        }
        if (TextUtils.isEmpty(age)) {
            showMessage(getString(R.string.age_hint));
            return;
        }
        if (TextUtils.isEmpty(gender)) {
            showMessage(getString(R.string.sex_hint));
            return;
        }
        if (TextUtils.isEmpty(city)) {
            showMessage(getString(R.string.region_hint));
            return;
        }
        if (!TextUtils.isEmpty(weight)) {
            submit(name, weight);
        } else {
            submit(name, "");
        }
    }

    private void submit(String name, String weight) {
        perfectRegisterPresenter.perfectRegister(name, mAge, mGender, weight, dataBinding.etCity.getText().toString());

    }

    @Override
    public void success(String message) {
        showMessage(getString(R.string.success));
        startActivity(new Intent(this, MainActivity.class).putExtra("reg", 1));
        finish();
    }

    @Override
    public void failure(String message) {
        showMessage(message);
    }

}
